package org.blueoxygen.kukuruyuk.actions.item;
import java.sql.SQLException;
import org.blueoxygen.kukuruyuk.entity.Item;
import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.hibernate.HibernateException;

public class SearchItem extends ItemForm {
	
	private HibernateSessionFactory hsf;
		private String dropValue; 
		private Session sess;
		private int maxPage, currPage, nextPage, prevPage = 0, page = 0;
		private int maxRowPerPage = 10;
		private String orderBy = "name";
		private int resultRows;
		public String execute(){
			sess = hsf.createSession();
			Criteria crit = sess.createCriteria(Item.class);
			
			if(getDropValue() != null
					&& !"".equalsIgnoreCase(getDropValue())){
					crit.add(Expression.like("category", getDropValue(),
					MatchMode.ANYWHERE));
			}
			
			if(getItem().getName() != null &&
					!"".equalsIgnoreCase(getItem().getName())){
					crit.add(Expression.like("name", getItem().getName()));
			} 
						resultRows = crit.list().size();
						maxPage = resultRows / maxRowPerPage;
						prevPage = currPage - 1;
						nextPage = currPage + 1;
						page = currPage + 1;
						if (resultRows % maxRowPerPage == 0) maxPage = maxPage -1;
						setItems(crit.addOrder(Order.asc(orderBy))
						.setFirstResult(currPage * maxRowPerPage)
						.setMaxResults(maxRowPerPage)
						.list());
						try {
						hsf.endSession((sess));
						hsf.closeSession(sess);
						} catch (HibernateException e) {
						e.printStackTrace();
						} catch (SQLException e) {	
							e.printStackTrace();
						}
						return SUCCESS;
						}
		public String getDropValue() {
			return dropValue;
		}
		public void setDropValue(String dropValue) {
			this.dropValue = dropValue;
		}
		public int getMaxPage() {
			return maxPage;
		}
		public void setMaxPage(int maxPage) {
			this.maxPage = maxPage;
		}
		public int getCurrPage() {
			return currPage;
		}
		public void setCurrPage(int currPage) {
			this.currPage = currPage;
		}
		public int getNextPage() {
			return nextPage;
		}
		public void setNextPage(int nextPage) {
			this.nextPage = nextPage;
		}
		public int getPrevPage() {
			return prevPage;
		}
		public void setPrevPage(int prevPage) {
			this.prevPage = prevPage;
		}
		public int getPage() {
			return page;
		}
		public void setPage(int page) {
			this.page = page;
		}
		public int getMaxRowPerPage() {
			return maxRowPerPage;
		}
		public void setMaxRowPerPage(int maxRowPerPage) {
			this.maxRowPerPage = maxRowPerPage;
		}
		public String getOrderBy() {
			return orderBy;
		}
		public void setOrderBy(String orderBy) {
			this.orderBy = orderBy;
		}
		public int getResultRows() {
			return resultRows;
		}
		public void setResultRows(int resultRows) {
			this.resultRows = resultRows;
		}
		public HibernateSessionFactory getHsf() {
			return hsf;
		}
		public void setHsf(HibernateSessionFactory hsf) {
			this.hsf = hsf;
		}
		
}